package cn.sowfin.controller;

import cn.sowfin.entity.User;
import cn.sowfin.jpa.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class  DataController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    UserRepository userRepository;



    @GetMapping("/api/data")
    public Map<String,Object> show(){

        return jdbcTemplate.queryForMap("select * from user limit 1");

    }
    @GetMapping("/api/user/{id}")
    public Map<String,Object> showUser(@PathVariable("id") Integer id){

        User user = userRepository.getOne(id);
        Map<String,Object> bean=new HashMap<>();
        bean.put("id",user.getId());
        bean.put("username",user.getUsername());
        bean.put("pw",user.getPassword());
        return bean;

    }
    @GetMapping("/api/user/u/{username}")
    public List<Map<String,Object>> findByUserName(@PathVariable("username") String username){

        List<User> users = userRepository.findByUsername(
                username);
        Map<String,Object> bean=new HashMap<>();
        for(User user:users){
            bean.put("id",user.getId());
            bean.put("username",user.getUsername());
        }
        List<Map<String,Object>> beans=new ArrayList<>();
        beans.add(bean);



        return beans;

    }


}
